package com.cheli.center.controller;


import com.cheli.center.entity.ManaStaff;
import com.cheli.center.entity.vo.LoginVo;
import com.cheli.center.service.ManaStaffService;
import com.cheli.commonutils.JwtUtils;
import com.cheli.commonutils.R;
import com.cheli.servicebase.config.exceptionhandler.MyException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 员工 前端控制器
 * </p>
 *
 * @author cheli
 * @since 2021-05-02
 */
@RestController
@RequestMapping("/ucenter/user")
@CrossOrigin
public class ManaStaffController {
    @Autowired
    private ManaStaffService manaStaffService;

    @PostMapping("login")
    public R loginUser(@RequestBody LoginVo loginVo){
         String token = manaStaffService.login(loginVo);
        return R.ok().data("token",token);
    }

   @PostMapping("updatePass")
    public R updatePass(LoginVo loginVo) {

        String id = loginVo.getId();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(id) || StringUtils.isEmpty(password)){
            throw new MyException(20001, "注册失败");
        }
        ManaStaff staff = manaStaffService.getById(id);
        staff.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        manaStaffService.save(staff);
        return R.ok();
    }

    @GetMapping("getInfo")
    public R getInfo(HttpServletRequest request){
       String id = JwtUtils.getMemberIdByJwtToken(request);
       ManaStaff user = manaStaffService.getById(id);
        return R.ok().data("userInfo", user);
    }
}

